package com.learn.utils;

import com.learn.dto.base.TokenData;
import org.apache.commons.lang3.StringUtils;

/**
 * token工具类
 *
 * @author yaoqihui
 * @version 2025/3/28
 * @see [相关类/方法]
 * @since [learn-platform]
 */
public class TokenUtils
{

	/**
	 * 从请求头里获取管理后台token
	 *
	 * @return TokenData对象，不会返回null
	 */
	public static TokenData getAdminTokenFromHeader (String authorization)
	{
		// 从Authorization请求头获取
		if (StringUtils.isNotEmpty (authorization))
		{
			// 解析请求头
			String[] tokenSplitRes = authorization.split (" ");
			int tokenLength = 2;
			if (tokenSplitRes.length != tokenLength)
			{
				return new TokenData ().setToken (authorization);
			}
			return new TokenData ().setToken (tokenSplitRes[1]).setApplyType (tokenSplitRes[0]);
		}
		return new TokenData ();
	}
}